package edu.scut.data;

import static org.junit.Assert.*;

import org.interpss.display.AclfOutFunc;
import org.junit.Test;

import com.interpss.common.datatype.UnitType;
import com.interpss.core.CoreObjectFactory;
import com.interpss.core.aclf.AclfNetwork;
import com.interpss.core.algo.LoadflowAlgorithm;
import com.interpss.core.net.Branch;

// TODO: 检查读到的电阻值不对
public class DistributionNetworkImporterTest {

	// 测试DistributionNetworkImporter的构造函数
	@Test
	public void testDistributionNetworkImporter() {
		// 1. 生成网络
		AclfNetwork ieeeNetwork = new DistributionNetworkImporter("testData/ieee70cdf.xls").getNetwork();
		// 2. 计算潮流
	  	LoadflowAlgorithm algo = CoreObjectFactory.createLoadflowAlgorithm(ieeeNetwork);
	  	algo.loadflow();
	  	System.out.println(AclfOutFunc.loadFlowSummary(ieeeNetwork));
	  	System.out.println(ieeeNetwork.net2String());
	  	// 3. 计算网损
	  	float loss = 0;
	  	for (Branch thisBranch : ieeeNetwork.getBranchList()) {
	  		double thisCurrent = ieeeNetwork.getAclfBranch(thisBranch.getId()).current(UnitType.Amp);
	  		double thisVB = ieeeNetwork.getAclfBus(ieeeNetwork.getAclfBranch(thisBranch.getId()).getFromBusId()).getBaseVoltage() / 1000.0;
	  		double thisSB = ieeeNetwork.getBaseKva() / 1000.0;
	  		double thisR = ieeeNetwork.getAclfBranch(thisBranch.getId()).getZ().getReal() * thisVB * thisVB / thisSB;
	  		loss += thisCurrent * thisCurrent * thisR;
	  	}
	  	System.out.println("Total loss = " + loss);
		assertTrue(loss > 0);
	}

}
